<?php
/**
 * @file Diseasem.php
 * @synopsis  疾病
 * @author Yee, <rlk002@gmail.com>
 * @version 1.0
 * @date 2015-03-13 16:47:32
 */

defined('BASEPATH') OR exit('No direct script access allowed');

class Diseasem extends CI_Model
{
    function __construct()
    {

    }

    public function get_list_by_part($disease_part = '')
    {
        $data = array();
        $cache_name = 'diseaseListByPart'.$disease_part;
        $data = $this->cache->get($cache_name);
        if($data)
        {
            return $data;
        }

        $query_type = $this->db->select()->from('ml_disease_type')->where('is_show',1)->order_by('sort')->get()->result_array();
        foreach($query_type AS $row)
        {
            $disease_type_data = array();
            $disease_type_data['type_name'] = $row['ch_disease_type_name'];
            $this->db->select('ml_disease.id,ml_disease.ch_disease_name,ml_disease.en_disease_name')->from('ml_disease');
            $this->db->join('ml_disease_part','ml_disease.disease_part=ml_disease_part.id','left');
            $this->db->join('ml_disease_type','ml_disease.disease_type=ml_disease_type.id','left');
            $this->db->where('ml_disease.disease_type',$row['id']);
            $disease_part && $this->db->where('ml_disease.disease_part',$disease_part);
            $this->db->where('ml_disease.is_show',1);
            $this->db->order_by('CONVERT( ml_disease.ch_disease_name USING gbk ) COLLATE gbk_chinese_ci');
            $rquery = $this->db->get()->result_array();
            foreach($rquery AS $disease_data)
            {
                $disease_data_content = array();
                $disease_data_content['id'] = $disease_data['id'];
                $disease_data_content['ch_disease_name'] = $disease_data['ch_disease_name'] ? $disease_data['ch_disease_name'] : "";
                $disease_data_content['en_disease_name'] = $disease_data['en_disease_name'] ? $disease_data['en_disease_name'] : "";
                $disease_type_data['disease_list'][] = $disease_data_content;
            }
            if(isset($disease_type_data['disease_list'])) //有疾病的时候才有类别
            {
                $data[] = $disease_type_data;
            }
        }

        $this->cache->save($cache_name,$data,86400);
        return $data;
    }


    public function get_list_by_thread($disease_part = '')
    {
        $data = array();
        $cache_name = 'diseaseListByThread'.$disease_part;
        $data = $this->cache->get($cache_name);
        if($data)
        {
            return $data;
        }

        $query_type = $this->db->select()->from('ml_disease_type')->where('is_show',1)->order_by('sort')->get()->result_array();

        foreach($query_type AS $row)
        {
            $disease_type_data = array();
            $disease_type_data['type_name'] = $row['ch_disease_type_name'];
            $this->db->select('ml_disease.id,ml_disease.ch_disease_name,ml_disease.en_disease_name')->from('ml_disease');
            $this->db->join('ml_disease_thread','ml_disease.disease_thread=ml_disease_thread.id','left');
            $this->db->join('ml_disease_type','ml_disease.disease_type=ml_disease_type.id','left');
            $this->db->where('ml_disease.disease_type',$row['id']);
            $disease_part && $this->db->where('ml_disease.disease_thread',$disease_part);
            $this->db->where('ml_disease.is_show',1);
            $this->db->order_by('CONVERT( ml_disease.ch_disease_name USING gbk ) COLLATE gbk_chinese_ci');
            $rquery = $this->db->get()->result_array();

            foreach($rquery AS $disease_data)
            {
                $disease_data_content = array();
                $disease_data_content['id'] = $disease_data['id'];
                $disease_data_content['ch_disease_name'] = $disease_data['ch_disease_name'] ? $disease_data['ch_disease_name'] : "";
                $disease_data_content['en_disease_name'] = $disease_data['en_disease_name'] ? $disease_data['en_disease_name'] : "";
                $disease_type_data['disease_list'][] = $disease_data_content;
            }
            if(isset($disease_type_data['disease_list'])) //有疾病的时候才有类别
            {
                $data[] = $disease_type_data;
            }
        }

        $this->cache->save($cache_name,$data,86400);
        return $data;
    }


    public function get_list_by_disubject($disease_part = '')
    {
        $data = array();
        $cache_name = 'diseaseListBySubject'.$disease_part;
        $data = $this->cache->get($cache_name);
        if($data)
        {
            return $data;
        }

        //获取分类模型下的分类里面的  疾病分类  然后再获取每个最底层分类的疾病列表
        $query_type = $this->db->select()->from('ml_disease_type')->where('is_show',1)->order_by('sort')->get()->result_array();

        foreach($query_type AS $row)
        {
            $disease_type_data = array();
            $disease_type_data['type_name'] = $row['ch_disease_type_name'];
            $this->db->select('ml_disease.id,ml_disease.ch_disease_name,ml_disease.en_disease_name')->from('ml_disease');
            $this->db->join('ml_disease_subject','ml_disease.disease_subject=ml_disease_subject.id','left');
            $this->db->join('ml_disease_type','ml_disease.disease_type=ml_disease_type.id','left');
            $this->db->join('ml_diseasesubject','ml_disease.id=ml_diseasesubject.disease_id','left');
            $this->db->where('ml_disease.disease_type',$row['id']);
            $disease_part && $this->db->where('ml_diseasesubject.subject',$disease_part);
            $this->db->where('ml_disease.is_show',1);
            $this->db->order_by('CONVERT( ml_disease.ch_disease_name USING gbk ) COLLATE gbk_chinese_ci');
            $rquery = $this->db->get()->result_array();

            foreach($rquery AS $disease_data)
            {
                $disease_data_content = array();
                $disease_data_content['id'] = $disease_data['id'];
                $disease_data_content['ch_disease_name'] = $disease_data['ch_disease_name'] ? $disease_data['ch_disease_name'] : "";
                $disease_data_content['en_disease_name'] = $disease_data['en_disease_name'] ? $disease_data['en_disease_name'] : "";
                $disease_type_data['disease_list'][] = $disease_data_content;
            }
            if(isset($disease_type_data['disease_list'])) //有疾病的时候才有类别
            {
                $data[] = $disease_type_data;
            }
        }

        $this->cache->save($cache_name,$data,86400);
        return $data;
    }

    public function get_disease_case_imgs_list($disease_id,$num)
    {
        $this->db->select('case_imgs,case_imgs_des')->from('ml_disease_case');
        $this->db->where('disease_id',$disease_id);
        $this->db->where('case_num',$num);
        $this->db->where('is_show',1);
        $query = $this->db->get()->result_array();
        $data = array();
        foreach($query AS $value)
        {
            $case_imgs_result['case_imgs'] = ret_fullpath_url($value['case_imgs']);
            $case_imgs_result['case_imgs_des'] = $value['case_imgs_des'];
            $data[] = $case_imgs_result;
        }
        return $data;
    }

    public function get_content($disease_id)
    {
        $disease = array();
        $disease_case_content = array();
        $this->db->select()->from('ml_disease_case');
        $this->db->join('ml_fellow','ml_disease_case.author_id=ml_fellow.id','left');
        $this->db->where('ml_disease_case.is_show',1);
        $this->db->where('ml_disease_case.disease_id',$disease_id);
        $this->db->group_by('ml_disease_case.case_num');
        $lists = $this->db->get()->result_array();
        $i = 0;
        foreach($lists AS $row_disease_content)
        {
            $disease_case_content[$i]['case_name'] = "case" . ($i + 1);
            $disease_case_content[$i]['case_author'] = $row_disease_content['username'];
            $disease_case_content[$i]['case_author_id'] = $row_disease_content['author_id'];
            $disease_case_content[$i]['aspect_ratio'] = $row_disease_content['aspect_ratio'];
            $disease_case_content[$i]['case_imgs'] = $this->get_disease_case_imgs_list($disease_id,$row_disease_content['case_num']);
            $i++;
        }
        $disease_attr_content = $this->db->select()->from('ml_disease')->where('id',$disease_id)->get()->result_array()[0];
        $en_disease_name = explode('，', $disease_attr_content['en_disease_name']);
        $en_disease_name = $en_disease_name[0] ? '，'.$en_disease_name[0] : ""; //如果有英文

        $disease['disease_case_content'] = $disease_case_content;

        $disease['disease_name'] = $disease_attr_content['ch_disease_name'];

        $disease['attr'][] = array(
            "name" => "概述",
            "content" => $disease_attr_content['summary'] ? $disease_attr_content['summary'] : "",
            "key" => $disease_attr_content['ch_disease_name'] . $en_disease_name
        );
        $disease['attr'][] = array(
            "name" => "病因",
            "content" => $disease_attr_content['cause'] ? $disease_attr_content['cause'] : "",
            "key" => ""
        );
        $disease['attr'][] = array(
            "name" => "流行病学",
            "content" => $disease_attr_content['epidemiology'] ? $disease_attr_content['epidemiology'] : "",
            "key" => ""
        );
        $disease['attr'][] = array(
            "name" => "临床表现",
            "content" => $disease_attr_content['clinical_manifestation'] ? $disease_attr_content['clinical_manifestation'] : "",
            "key" => ""
        );
        $disease['attr'][] = array(
            "name" => "影像表现",
            "content" => $disease_attr_content['imaging_manifestations'] ? $disease_attr_content['imaging_manifestations'] : "",
            "key" => ""
        );
        $disease['attr'][] = array(
            "name" => "诊断",
            "content" => $disease_attr_content['diagnosis'] ? $disease_attr_content['diagnosis'] : "",
            "key" => ""
        );
        $disease['attr'][] = array(
            "name" => "鉴别诊断",
            "content" => $disease_attr_content['image_identification'] ? $disease_attr_content['image_identification'] : "",
            "key" => $disease_attr_content['identifi_keys'] ? $disease_attr_content['identifi_keys'] : ""
        );
        $disease['attr'][] = array(
            "name" => "治疗",
            "content" => $disease_attr_content['treat'] ? $disease_attr_content['treat'] : "",
            "key"=> ""
        );
        $disease['attr'][] = array(
            "name" => "预后",
            "content" => $disease_attr_content['after_healing'] ? $disease_attr_content['after_healing'] : "",
            "key" => ""
        );
        $disease['attr'][] = array(
            "name" => "关键词",
            "content" => $disease_attr_content['disease_keys'] ? $disease_attr_content['disease_keys'] : "",
            "key" => ""
        );
        $disease['attr'][] = array(
            "name" => "参考文献",
            "content" => $disease_attr_content['reference'],
            "key" => ""
        );
        return $disease;
    }

    public function get_disease_part_list()
    {
        $data = array();
        /*$data = $this->cache->get('diseasePartList');
        if($data)
        {
            return $data;
        }*/

        $this->db->select()->from('ml_disease_part');
        $this->db->order_by('sort','asc');
        $query = $this->db->get()->result_array();
        if($query)
        {
            foreach($query AS $key => $value)
            {
                $item = array();
                $item['images_thumb'] = ret_fullpath_url($value['images_thumb']);
                $item['ch_part_name'] = $value['ch_part_name'];
                $item['en_part_name'] = $value['en_part_name'];
                $item['id'] = $value['id'];
                $data[] = $item;
            }
        }

        //$this->cache->save('diseasePartList',$data,86400);
        return $data;
    }

    public function get_disease_thread_list()
    {
        $data = array();
        /*$data = $this->cache->get('diseaseThreadList');
        if($data)
        {
            return $data;
        }*/

        $this->db->select()->from('ml_disease_thread');
        $this->db->order_by('sort','asc');
        $query = $this->db->get()->result_array();
        if($query)
        {
            foreach($query AS $key => $value)
            {
                $item = array();
                $item['images_thumb'] = ret_fullpath_url($value['images_thumb']);
                $item['ch_part_name'] = $value['ch_thread_name'];
                $item['en_part_name'] = $value['en_thread_name'];
                $item['id'] = $value['id'];
                $data[] = $item;
            }
        }

        //$this->cache->save('diseaseThreadList',$data,86400);
        return $data;
    }


    public function get_disease_subject_list()
    {
        $data = array();
        /*$data = $this->cache->get('diseaseThreadList');
        if($data)
        {
            return $data;
        }*/

        $this->db->select()->from('ml_disease_subject');
        $this->db->order_by('sort','asc');
        $query = $this->db->get()->result_array();
        if($query)
        {
            foreach($query AS $key => $value)
            {
                $item = array();
                $item['images_thumb'] = ret_fullpath_url($value['images_thumb']);
                $item['ch_part_name'] = $value['ch_subject_name'];
                $item['en_part_name'] = $value['en_subject_name'];
                $item['id'] = $value['id'];
                $data[] = $item;
            }
        }

        //$this->cache->save('diseaseThreadList',$data,86400);
        return $data;
    }

    public function insert($indata)
    {
        $this->db->insert('ml_disease_case',$indata);
    }


    public function get_search_list($search_type,$search_query)
    {
        $query_type = $this->db->select()->from('ml_disease_'.$search_type)->where('is_show',1)->order_by('sort')->get()->result_array();

        $data = [];
        if($query_type)
        {
            foreach($query_type AS $row)
            {
                $disease_type_data = array();
                $disease_type_data['type_name'] = $row['ch_'.$search_type.'_name'];
                $this->db->select('ml_disease.id,ml_disease.ch_disease_name,ml_disease.en_disease_name')->from('ml_disease');
                $this->db->join('ml_disease_'.$search_type.'','ml_disease.disease_'.$search_type.'=ml_disease_'.$search_type.'.id','left');
                $this->db->where('ml_disease.disease_'.$search_type,$row['id']);
                $this->db->where('ml_disease.is_show',1);
                $this->db->group_start();
                $this->db->like('ml_disease.ch_disease_name',$search_query);
                $this->db->or_like('ml_disease.en_disease_name', $search_query);
                $this->db->group_end();
                $this->db->order_by('CONVERT( ml_disease.ch_disease_name USING gbk ) COLLATE gbk_chinese_ci');
                $rquery = $this->db->get()->result_array();

                foreach($rquery AS $disease_data)
                {
                    $disease_data_content = array();
                    $disease_data_content['id'] = $disease_data['id'];
                    $disease_data_content['ch_disease_name'] = $disease_data['ch_disease_name'] ? $disease_data['ch_disease_name'] : "";
                    $disease_data_content['en_disease_name'] = $disease_data['en_disease_name'] ? $disease_data['en_disease_name'] : "";
                    $disease_type_data['disease_list'][] = $disease_data_content;
                }
                if(isset($disease_type_data['disease_list'])) //有疾病的时候才有类别
                {
                    $data[] = $disease_type_data;
                }
            }
        }

        return $data;

    }

    //subject学科分类 不是一样的类型 需要新写一个函数
    public function get_search_list3($search_type,$search_query)
    {
        $query_type = $this->db->select()->from('ml_disease_'.$search_type)->where('is_show',1)->order_by('sort')->get()->result_array();

        $data = [];
        if($query_type)
        {
            foreach($query_type AS $row)
            {
                $disease_type_data = array();
                $disease_type_data['type_name'] = $row['ch_'.$search_type.'_name'];
                $this->db->select('ml_disease.id,ml_disease.ch_disease_name,ml_disease.en_disease_name')->from('ml_disease');
                $this->db->join('ml_diseasesubject'.'','ml_diseasesubject.disease_id'.'=ml_disease.id','left');
                $this->db->where('ml_diseasesubject.subject',$row['id']);
                $this->db->where('ml_disease.is_show',1);
                $this->db->group_start();
                $this->db->like('ml_disease.ch_disease_name',$search_query);
                $this->db->or_like('ml_disease.en_disease_name', $search_query);
                $this->db->group_end();
                $this->db->order_by('CONVERT( ml_disease.ch_disease_name USING gbk ) COLLATE gbk_chinese_ci');
                $rquery = $this->db->get()->result_array();

                foreach($rquery AS $disease_data)
                {
                    $disease_data_content = array();
                    $disease_data_content['id'] = $disease_data['id'];
                    $disease_data_content['ch_disease_name'] = $disease_data['ch_disease_name'] ? $disease_data['ch_disease_name'] : "";
                    $disease_data_content['en_disease_name'] = $disease_data['en_disease_name'] ? $disease_data['en_disease_name'] : "";
                    $disease_type_data['disease_list'][] = $disease_data_content;
                }
                if(isset($disease_type_data['disease_list'])) //有疾病的时候才有类别
                {
                    $data[] = $disease_type_data;
                }
            }
        }

        return $data;

    }

}
